﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Web.UI;

using Microsoft.Security.Application;

using IdeaNMR.Web.Core;
using IdeaNMR.Shared.Utils;

namespace IdeaNMR.Web
{
    /// <summary>
    /// Because NMRObject entities can have different version belonging to different authors
    /// this control should not be usd to get NMRObject versions because user can get an access
    /// to the version that he is not an author of.
    /// This control works on HardObject, so it does not validate the type of versions
    /// </summary>
    /// <remarks>
    /// <para>Since this control anyways dynamicaly creates all the stuff, it is not using ViewState.</para>
    /// </remarks>
    public partial class VersionList : System.Web.UI.UserControl
    {
        /// <summary>
        /// A CssClass that will be added as prefix to every internal control that has CssClass specified.
        /// </summary>
        public string CssClass { get; set; }

        /// <summary>
        /// A list of Versions to Display
        /// </summary>
        public System.Data.Objects.DataClasses.EntityCollection<BaseObject> Versions
        { get; set; }

        ///// <summary>
        ///// A <see cref="System.Type"/> of <see cref="IdeaNMR.Web.VersionList.Versions"/> item.
        ///// </summary>
        //public Type ObjectType
        //{ get; set; }


        protected string HyperLinkFormat
        {
            get
            {
                return string.Format("<b>{{0}}</b> from <i>{{1:{0}}}</i>", ScrewTurnWikiFacade.DateTimeFormat);
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            panMain.CssClass = this.CssClass;

            lvVersions.DataSource = this.Versions.OrderByDescending(x => x.Datetime);
            lvVersions.DataBind();
        }

        protected string CreateUrl(string targetId)
        {
            UriBuilder uri = new UriBuilder(Page.Request.Url.AbsoluteUri);
            uri.Query = null;
            uri.AddQueryParameter("TargetID", targetId.ToString());

            return uri.Uri.ToString();
        }
    }
}